<?php

namespace anpai\php\Imp_manager;
header("Content-type:text/html;charset=utf-8");
require_once 'Medoo.php';
require_once 'Contact.php';
/**
 * 监控闪拍截图  文件上传
 * name
 */
upload_logo();
function upload_logo()
{
    $file = $_FILES['name'];//得到传输的数据
    $name = $file['name'];
    /** 获取扩展名 */
    $file_ext = pathinfo($name);
    $file_ext = $file_ext['extension'];
    $file_ext = strtolower($file_ext); // 统一将文件扩展名转成小写

    $conf_ext = ['gif', 'jpg', 'jpeg', 'png'];

    if (!in_array($file_ext, $conf_ext)) {
        return '对不起文件格式错误';
    }

    /** 获取新的文件名 */
    $str = '123456789abcdefhijkmnpqrstuvwxyz'; //去掉容易混淆的l,o,g字母(共32个)
    $file_name = '';
    for ($i = 0; $i < 20; $i++) {
        $file_name .= $str[mt_rand(0, strlen($str) - 1)];
    }

    /** 设定存储目录：u/app_logo */
    $dir_root = str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']);
    if (!file_exists("up_img")) {
        mkdir("up_img");
    }
    if (!file_exists("up_img/device")) {
        mkdir("up_img/device");
    }

    $target_file = "up_img/device/{$file_name}.{$file_ext}";
    $info = move_uploaded_file($file['tmp_name'], $target_file);
    if ($info) {
        //$image = \think\Image::open($target_file);
        //$image->thumb(480, 270, \Think\Image::THUMB_FIXED)->save($target_file);
        $path = "up_img/device/{$file_name}.{$file_ext}";
        insertDB($path);
        echo(json_encode(["code" => 200, "msg" => 'logo', 'data' => $path]));
    } else {
        echo(json_encode(["code" => 1, "msg" => '重新上传', 'data' => '']));
    }
}

/**
 * @param $path
 * 插入数据库
 */
function insertDB($path)
{
    global $database;
    $database->insert('manage_monitoring_log', [
        'log_id' => '1',
        'modular_id' => '104',
        'happen_time' => '2017年12月14日 23：23：89',
        'divice_id' => 'M012',
        'pic_size' => '342 KB',
        'pic_resolution' => '800 * 480',
        'shutter_speed' => '1/1000 /s',
        'img' => $path,
        'is_look' => '1'
    ]);
}

